//
//  shiPinTableViewCell.m
//  ProJectB_1
//
//  Created by lanou3g on 16/8/15.
//  Copyright © 2016年 yangshukai. All rights reserved.
//

#import "shiPinTableViewCell.h"
#import "Masonry.h"
#import "ShiPinButton.h"
#import "UIImageView+WebCache.h"
#import "Header.h"
#import <AVFoundation/AVFoundation.h>

@interface shiPinTableViewCell ()

@property (nonatomic,strong) UIImageView *profile_image;
@property (nonatomic,strong) UILabel *name;
@property (nonatomic,strong) UILabel *passtime;


// 自定义button
@property (nonatomic,strong) ShiPinButton *dingButton;
@property (nonatomic,strong) ShiPinButton *caiButton;
@property (nonatomic,strong) ShiPinButton *repostButton;
@property (nonatomic,strong) ShiPinButton *commentButton;

@property (nonatomic,strong) UILabel *videoTimeLable;
@property (nonatomic,strong) UILabel *playTime;

@property (nonatomic,strong) AVPlayer *audioPlayer;

@end

@implementation shiPinTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        /// 添加控件
        self.profile_image = [[UIImageView alloc]init];
        _profile_image.layer.cornerRadius = 20;
        _profile_image.layer.masksToBounds = YES;
        [self.contentView addSubview:_profile_image];
        
        self.name = [[UILabel alloc]init];
        
        _name.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_name];
        
        self.passtime = [[UILabel alloc]init];
        _passtime.font = [UIFont systemFontOfSize:14];
        
        [self.contentView addSubview:_passtime];
        
        self.text = [[UILabel alloc]init];
        _text.font = [UIFont systemFontOfSize:14];
        _text.numberOfLines = 0;
        [self.contentView addSubview:self.text];
        
        self.image1 = [[UIImageView alloc]init];
        _image1.backgroundColor = [UIColor lightGrayColor];
        _image1.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_image1];
        
        self.dingButton = [[ShiPinButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self.contentView addSubview:_dingButton];
        
        self.caiButton = [[ShiPinButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self.contentView addSubview:_caiButton];
        
        self.repostButton = [[ShiPinButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self.contentView addSubview:_repostButton];
        
        self.commentButton = [[ShiPinButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self.contentView addSubview:_commentButton];
        
        self.videoTimeLable = [[UILabel alloc]init];
        _videoTimeLable.alpha = 0.5;
        _videoTimeLable.backgroundColor = [UIColor lightGrayColor];
        [_image1 addSubview:_videoTimeLable];
        
        self.playTime = [[UILabel alloc]init];
        _playTime.alpha = 0.5;
        _playTime.backgroundColor = [UIColor lightGrayColor];
        [_image1 addSubview:_playTime];
        
#pragma mark---播放器
        
        
    }
    return self;
}




// Masonry约束
- (void)layoutSubviews {
    __weak typeof(self)weakSelf = self;
#pragma mark ---profile
    [self.profile_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(4);
        make.left.offset(8);
        make.width.offset(40);
        make.height.offset(40);
    }];
    
#pragma mark --name
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.profile_image.mas_right).offset(4);
        make.top.equalTo(weakSelf.profile_image.mas_top).offset(0);
        make.height.offset(20);
#warning 没有设置高度
    }];
#pragma mark ---passTime
    [self.passtime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.profile_image.mas_right).offset(4);
        make.top.equalTo(weakSelf.profile_image.mas_top).offset(20);
        make.height.offset(20);
#warning 没有设置高度
    }];
    
#pragma mark ---text
//    [self.text mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(weakSelf.profile_image.mas_bottom).offset(10);
//        make.left.offset(10);
//        make.right.offset(-10);
    
    self.text.frame = CGRectMake(10, 0, [UIScreen mainScreen].bounds.size.width - 20, 20);
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
    
    
    NSString *text = weakSelf.shiPinModelU.text;
    
    CGSize retSize = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, 10000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil].size;
    
    CGFloat yyy = retSize.height;
    CGRect rect = weakSelf.text.frame;
    rect.origin.x = 10;
    rect.origin.y = 50;
    rect.size.height = yyy;
    
    weakSelf.text.frame = rect;
    
#pragma mark ----image1
    [self.image1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.text.mas_bottom).offset(10);
        make.left.offset(10);
        make.right.offset(-10);
        make.height.offset((weakSelf.bounds.size.width - 20) / 5 * 3);
    }];
    
    [self.dingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.image1.mas_bottom).offset(10);
        make.left.offset(0);
        make.bottom.offset(0);
        make.height.offset(selfSizeWidth * 10);
        make.width.offset(selfSizeWidth * 25);
    }];
    
    [self.caiButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.image1.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.dingButton.mas_right).offset(0);
        make.bottom.offset(0);
        make.height.offset(selfSizeWidth * 10);
        make.width.offset(selfSizeWidth * 25);
    }];
    
    [self.repostButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.image1.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.caiButton.mas_right).offset(0);
        make.bottom.offset(0);
        make.height.offset(selfSizeWidth * 10);
        make.width.offset(selfSizeWidth * 25);
    }];
    
    [self.commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.image1.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.repostButton.mas_right).offset(0);
        make.bottom.offset(0);
        make.height.offset(selfSizeWidth * 10);
        make.width.offset(selfSizeWidth * 25);
    }];
    
    
    [_playTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_image1.mas_left).offset(0);
        make.bottom.equalTo(_image1.mas_bottom).offset(0);
        make.height.offset(selfSizeHeight * 9);
//        make.width.offset(selfSizeWidth * 15);
    }];
    
    [_videoTimeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_image1.mas_right).offset(0);
        make.bottom.equalTo(_image1.mas_bottom).offset(0);
        make.height.offset(selfSizeHeight * 9);
//        make.width.offset(selfSizeWidth * 15);
    }];
    
    
    
#pragma mark ---赋值
     [_profile_image sd_setImageWithURL:[NSURL URLWithString:self.shiPinModelU.profile_image]];
    
    _name.text = self.shiPinModelU.name;
    _passtime.text = self.shiPinModelU.passtime;
    
     _text.text = self.shiPinModelU.text;
    
    [_image1 sd_setImageWithURL:[NSURL URLWithString:self.shiPinModelU.image1]];
    _dingButton.numberLabel.text = self.shiPinModelU.ding;
    _dingButton.imageName = @"zan";
    _caiButton.numberLabel.text = self.shiPinModelU.cai;
    _caiButton.imageName = @"cai";
    _repostButton.numberLabel.text = self.shiPinModelU.repost;
    _repostButton.imageName = @"pingLun";
    _commentButton.numberLabel.text = self.shiPinModelU.comment;
    _commentButton.imageName = @"comment";
    
    NSInteger time = [self.shiPinModelU.videotime integerValue];
    
    _videoTimeLable.text = [NSString stringWithFormat:@"%.2ld:%.2ld  ", time / 60,time % 60];
    
    _playTime.text = [NSString stringWithFormat:@"  播放%ld次", [self.shiPinModelU.playcount integerValue]];
    
}

@end
